/* QtLua -- Lua bindings for Qt
   Copyright (C) 2011, Jarek Pelczar

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library; if not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301 USA

*/

#ifndef QLUASCRIPTCONTEXT_P_H
#define QLUASCRIPTCONTEXT_P_H

#include "qluascriptcontext.h"
#include "qluascriptvalue.h"
#include "qluascriptcontextinfo.h"
#include <QVector>
#include <QPointer>
#include "../etc/lua.hpp"

class QLuaScriptEnginePrivate;
class QLuaScriptable;
class QLuaScriptContextInfoPrivate;

class QLuaScriptContextPrivate
{
public:
    QLuaScriptContext * m_parent;
    QLuaScriptEngine * m_engine;
    QLuaScriptValue m_thisObject;
    QVector<QLuaScriptValue> m_args;
    QLuaScriptContextInfoPrivate * m_contextInfo;
    QPointer<QObject> m_scriptableObject;
    QLuaScriptEngine * m_prevScriptableEngine;
    QLuaScriptContext * m_prevScriptableContext;
    int m_activationRecordId;

    QLuaScriptContextPrivate() {
        m_contextInfo = 0;
        m_activationRecordId = 0;
    }

    ~QLuaScriptContextPrivate();
    void setupInfo(lua_Debug * ar);

    static inline QLuaScriptContextPrivate * priv(QLuaScriptContext * ctx)
    {
        return ctx->d;
    }

    void pushRecord();
    void popRecord();
};

class QLuaScriptContextInfoPrivate;

namespace QLuaInternal {

class QLuaScriptContextCleanup
{
public:
    QLuaScriptContextCleanup(QLuaScriptEnginePrivate * enginePrivate,
                             QLuaScriptContext * activeContext,
                             QVector<QLuaScriptValue> args,
                             const QLuaScriptValue& self,
                             QObject * scriptable);

    void overrideFunctionType(QLuaScriptContextInfo::FunctionType type);

private:
    QLuaScriptContextInfoPrivate * m_contextInfo;
};

}

#endif // QLUASCRIPTCONTEXT_P_H
